package com.black.platform.dictionary.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.common.web.PageQuery;
import com.black.platform.dictionary.po.DictionaryCategory;
import com.black.platform.dictionary.service.handler.DictionaryCategoryHandlerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dictionaryCategory")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictionaryCategoryController {

    private final DictionaryCategoryHandlerService dictionaryCategoryHandlerService;

    @PostMapping("search")
    public ApiResponse<IPage<DictionaryCategory>> search(@RequestBody PageQuery<DictionaryCategory> searchVo) {
        return ApiResponseBuilder.ok(dictionaryCategoryHandlerService.search(searchVo));
    }

    @GetMapping("Id/{id}")
    public ApiResponse<DictionaryCategory> getById(@PathVariable("id") Long id) {
        return ApiResponseBuilder.ok(dictionaryCategoryHandlerService.getById(id));
    }

    @PostMapping("save")
    public ApiResponse<Boolean> save(@RequestBody DictionaryCategory dictionaryCategory) {
        return ApiResponseBuilder.ok(dictionaryCategoryHandlerService.save(dictionaryCategory));
    }

    @PostMapping("update")
    public ApiResponse<Boolean> saveOrUpdate(@RequestBody DictionaryCategory dictionaryCategory) {
        return ApiResponseBuilder.ok(dictionaryCategoryHandlerService.update(dictionaryCategory));
    }

    @DeleteMapping()
    public ApiResponse<String> delete(@RequestBody List<Long> ids) {
        if (dictionaryCategoryHandlerService.deleteByIds(ids)) {
            return ApiResponseBuilder.ok("删除成功");
        } else {
            return ApiResponseBuilder.error("删除失败");
        }
    }
}
